<?php

namespace app\modules\api\controllers;

use app\components\XSecurity;
use app\services\UserService;
use app\models\Article;
use yii;
use app\extensions\ApiError;

class UserController extends BaseController
{
    /**
     * 注册用户
     */
    public function actionRegister()
    {
        $userName = Yii::$app->request->post('userName');
        $pwd      = Yii::$app->request->post('pwd');
        $rePwd    = Yii::$app->request->post('rePwd');
        
        
        $userService = new UserService();
        try{
            //注册用户
            $res = $userService->register($userName,$pwd,$rePwd);
        }catch(ApiError $e){
            $err = $e->_return();
            return $this->rspError($err->errCode, $err->errMsg);
        }
        if(isset($res["errCode"])){
            return $this->rspError($res["errCode"], $res["errMsg"]);
        }else{
            return $this->rspSuccess("注册成功",$res['data']);
        }

    }
    /**
     * 登录
     */
    public function actionLogin()
    {
        $userName = Yii::$app->request->post('userName');
        $pwd      = Yii::$app->request->post('pwd');
        $userService = new UserService();
        try{
            $res = $userService->login($userName,$pwd);
        }catch(ApiError $e){
            $err = $e->_return();
            return $this->rspError($err->errCode, $err->errMsg);
        }
        
        $this->rspSuccess("登录成功", $res["token"]);
    }
}
